package object.obj2D.graphics;

import java.util.Vector;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.Bitmap;

public class Font {
	
	public static final int ALIGN_LEFT = 0;
	public static final int ALIGN_RIGHT = 1;
	public static final int ALIGN_CENTER = 2;
	
	private Bitmap imgFont;
	private String stMap;
	private String stMap2;
	private byte[] charW;
	private int charH;
	private int spaceW;
	private int imgWidth;
	private int imgHeight;
	
	private int m_textureID;
	private Texture m_texture;
	private TexSquare m_texSquare = new TexSquare(12, 12);
	
	private int pos;

	//dw: default char width
	//cw: char width array
	//ch: char height
	//sw: space width
	//map: string map 1
	public Font(Bitmap bitmap, byte[] charwidth, int height, int spacewidth, String map)
	{		
		imgFont = bitmap;
		m_texture = new Texture();
		m_textureID = m_texture.initTexture(bitmap);
		charW = charwidth;
		charH = height;
		spaceW = spacewidth;
		stMap = map;
		
		imgWidth = imgFont.getWidth();
		imgHeight = imgFont.getHeight();
	}

	public int stringWidth(byte[] st)
	{
		int width = 0;
		int i;
		for(i = 0; i < st.length; i++)
		{
			if (st[i] >= 0)
				width += charW[st[i]];
			else
				width += spaceW;
		}
		return width;
	}
	
	public int substringWidth(byte[] st,int start,int len)
	{
		int width = 0;
		int end = start + len;
		if(end > st.length) end = st.length;
		for(int i = start;i < end && st[i]!= -3; i++)
		{
			if (st[i] >= 0)
				width += charW[st[i]];
			else
				width += spaceW;
		}
		return width;
	}
	
	public int stringWidth(String st)
	{
		return substringWidth(st, 0, st.length());
	}
	
	public int substringWidth(String st, int start, int len)
	{
		int width = 0;
		int end = start+len;
		if (end > st.length()) end = st.length();
		
		char ch;
		
		for(int i = start;i < end; i++)
		{
			ch = st.charAt(i);
			if(ch == '\n') break;
			width += charWidth(ch);
		}
		return width;
	}
	
	// unknow: -1
	// ' '   : -2
	// '\n'  : -3
	public byte[] toBytes(String st)
	{
		byte data[]=new byte[st.length()];
		for(int i=0;i<data.length;i++)
		{
			if (st.charAt(i)==' ') data[i]=-2;
			else if (st.charAt(i)=='\n') data[i]=-3;
			else
			{
				data[i]=(byte)stMap.indexOf(st.charAt(i));
				if (data[i]==-1 && stMap2!=null) data[i]=(byte)stMap2.indexOf(st.charAt(i));
			}
		}
		return data;
	}
	
	public void drawString(GL10 gl, String st, int x, int y, int align)
	{
		drawSubstring(gl, st, 0, st.length(), x, y, align);
	}
	
	public void drawSubstring(GL10 gl, String st, int start, int len, int x, int y, int align)
	{
		int x1, y1;
		int pos;
		int end = start + len;
		if(end > st.length()) end = st.length();
		
		if (align == 0) x1 = x;
		else if (align == 1) x1 = x - substringWidth(st, start, len);
		else x1 = x - (substringWidth(st, start, len) >> 1);
		y1 = y;
							
		char ch;
		int trans = 0;
		
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		m_texture.loadTexture(m_textureID);
		
		for(int i = start; i < end; ++i)
		{
			ch = st.charAt(i);
			if(ch == '\n')
			{
				trans = 0;
				
				if (align == 0)
				{
					x1 = x;
				}
				else if (align == 1)
				{
					x1 = x - substringWidth(st, i+1, len - i- start - 1);
				}
				else
				{
					x1 = x - (substringWidth(st, i + 1, len - i - start - 1) >> 1);
				}
				y1 += charH;
			}
			else if ((pos = charIndex(ch)) >= 0)
			{
				
				gl.glPushMatrix();
				
				gl.glTranslatef(x1, y1, 0);
				m_texSquare.SetTexCoord(new float[]{
						0, (float)pos*charH/imgHeight,
						(float)charW[pos]/ imgWidth, (float)pos*charH/imgHeight,
						(float)charW[pos]/imgWidth, (float)(pos + 1)*charH/imgHeight,
						0, (float)(pos + 1)*charH/imgHeight
						
				});
				m_texSquare.SetSize(charW[pos], charH);
				m_texSquare.Render();
				x1 += charW[pos];
				trans += charW[pos];
				
				gl.glPopMatrix();
			}
			else
			{
				x1 += spaceW;
			}
		}
		
		gl.glPopMatrix();
	}
	
	
	/*
	// Viet chu co gach duoi
	// 1: gach duoi
	// 2: gach giua
	public void drawStringL(String st,int x,int y,int align,int line, Graphics g)
	{
		int x1;
		
		if (align==0) x1=x;
		else if (align==1) x1=x-stringWidth(st);
		else x1=x-(stringWidth(st)>>1);
		
		if (line==1)
		{
			g.drawLine(x1,charH+y,x1+stringWidth(st)-1,charH+y);
		}
		drawString(st,x1,y,0,g);
		if (line==2)
		{
			g.drawLine(x1,charH/2+y,x1+stringWidth(st)-1,charH/2+y);
		}
	}
	
	public void drawString(String st,int x,int y,int align,Graphics g)
	{
		drawSubstring(st,0,st.length(),x,y,align,g);
	}
	
	public void drawSubstring(String st,int start,int len,int x,int y,int align,Graphics g)
	{
		int x1,y1,pos,end=start+len;
		if (end>st.length()) end=st.length();
		
		if (align==0) x1=x;
		else if (align==1) x1=x-substringWidth(st,start,len);
		else x1=x-(substringWidth(st,start,len)>>1);
		y1=y;
							
		char ch;
		for(int i=start;i<end;i++)
		{
			ch=st.charAt(i);
			if (ch=='\n')
			{
				if (align==0) x1=x;
				else if (align==1) x1=x-substringWidth(st,i+1,len-i-start-1);
				else x1=x-(substringWidth(st,i+1,len-i-start-1)>>1);
				y1+=charH;
			}
			else if ((pos=charIndex(ch))>=0)
			{				
				g.drawRegion(imgFont,0,pos*charH,imgWidth,charH,0,x1,y1,20);
				x1+=charW[pos];				
			}
			else
			{
				x1+=spaceW;
			}
		}
	}
	
	public void drawChar(char ch,int x,int y,int align,Graphics g)
	{
		if ((pos=charIndex(ch))>=0)
		{
			g.drawRegion(imgFont,0,pos*charH,imgWidth,charH,0,x,y,0);
		}
	}
	
	public void drawString(byte[] st,int x,int y,int align,Graphics g)
	{
		drawSubstring(st, 0, st.length, x, y, align, g);
	}
	
	public void drawSubstring(byte[] st,int start,int len,int x,int y,int align,Graphics g)
	{
		int x1,y1,end=start+len;
		if (end>st.length) end=st.length;
		
		if (align==0) x1=x;
		else if (align==1) x1=x-substringWidth(st,start,len);
		else x1=x-(substringWidth(st,start,len)>>1);
		y1=y;
		
		for(int i=start;i<end;i++)
		{
			if (st[i]==-3)
			{
				if (align==0) x1=x;
				else if (align==1) x1=x-substringWidth(st,i+1,len-i-start-1);
				else x1=x-(substringWidth(st,i+1,len-i-start-1)>>1);
				y1+=charH;
			}
			else if (st[i]>=0)
			{
				g.drawRegion(imgFont,0,st[i]*charH,imgWidth,charH,0,x1,y1,20);
				x1+=charW[st[i]];
			}
			else
			{
				x1+=spaceW;
			}
		}
	}
	*/
	
	public int getHeight()
	{
		return charH;
	}
	
	public int charIndex(char ch)
	{
		return stMap.indexOf(ch);
	}
	
	public int charWidth(char ch)
	{
		pos = charIndex(ch);
		if (pos!=-1) return charW[pos];
		return spaceW;
	}
	
	public String[] cut(String st,int width)
	{
		Vector<String> vector = new Vector<String>();
		int i=0,i1=0,w1=0,ispace=-1;
		char ch;
		while (i<st.length())
		{
			ch=st.charAt(i);
			if (ch==' ') ispace=i;
			w1+=charWidth(ch);
			if (w1>width || ch=='\n')
			{
				if (ch!='\n' && ispace!=-1) i=ispace;	
				vector.addElement(st.substring(i1, i));
				if (st.charAt(i)==' ' || ch=='\n') i++;
				i1=i;
				w1=0;
				ispace=-1;
			}
			else i++;
		}
		if (i>i1) vector.addElement(st.substring(i1, i)); // end line
		
		String[] a=new String[vector.size()];
		for (i=0;i<a.length;i++)
		{
			a[i]=(String)vector.elementAt(i);
		}
		return a;
	}
	
	public Bitmap Cor2Cor(Bitmap srcBmp, int[] srcColor, int[] destColor) {
		int width = srcBmp.getWidth();
		int height = srcBmp.getHeight();
		int n = width * height;

		int[] array = new int[n];
		srcBmp.getPixels(array, 0, width, 0, 0, width, height);
		if (srcColor != null && destColor != null) {
			for (int i = 0; i < n; ++i) {
				for (int j = 0; j < srcColor.length; j++) {
					if (array[i] == srcColor[j]) {
						if (j < destColor.length)
							array[i] = destColor[j];
						else
							array[i] = destColor[destColor.length - 1];
					}
				}
			}
		}
		return Bitmap.createBitmap(array, width, height,
				Bitmap.Config.ARGB_8888);
	}
	
	public Bitmap Cor2Cor(int srcColor, int destColor) {
		int width = imgFont.getWidth();
		int height = imgFont.getHeight();
		int n = width * height;

		int[] array = new int[n];
		imgFont.getPixels(array, 0, width, 0, 0, width, height);
		for (int i = 0; i < n; ++i) {
			if(array[i] == srcColor) array[i] = destColor;
		}
		return Bitmap.createBitmap(array, width, height,
				Bitmap.Config.ARGB_8888);
	}
}
